import {userlist,usercount} from '../../../request/api'

// 封装一个接口类型
interface Statarole{
    userList:Array<object>
}

const state = {
    userList:[],
     // pageSize 页面显示条数
     pageSize:2,
     // 页码
     pageNum:1,
     // 总条数
     count:0
}

const getters = {
    getUser(state:Statarole){
        return state.userList
    },
    getCount(state:any){
        return state.count
    },
    getPageSize(state:any){
        return state.pageSize
    }
}

const mutations = {
    GETUSER(state:Statarole,payload:any){
        state.userList = payload
    },
    // 总页码
    GETCOUNT(state:any,payload:any){
        state.count = payload
    },
    // 修改当前页
    GETPAGES(state:any,payload:any){
        state.pageNum = payload
    }
}

const actions = {
    // async getRoleActions({commit}:any){
    //     let res = await rolelist()
    //     console.log(res);
    //     commit('GETROLE',res.list)
    // }
    getUserActions(context:any){
        userlist({
            size:context.state.pageSize, //查询条数
            page:context.state.pageNum, //页码
        }).then(res => {
            // console.log(res,'user仓库');
            if(res.code == 200){
                let list:any = res.list ? res.list : []
                if(context.state.pageNum != 1 && list.length == 0){
                    context.dispatch('changePages',context.state.pageNum-1)
                }
                context.commit('GETUSER',res.list)
            }
            
        })
    },
    // 请求总页码数量
    async getUserCount(context:any){
        let res:any = await usercount()
        console.log(res,' 页码总数量');
        context.commit('GETCOUNT',res.list[0].total)
    },
    // 点击页码改变当前页码
    changePages(context:any,payload:boolean){
        context.commit('GETPAGES',payload)
        context.dispatch('getUserActions')
    }
}

export default {
    state,
    getters,
    mutations,
    actions,
    namespaced:true
}